package com.yc.library.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MyBatisHelper {


    private static SqlSessionFactory sqlSessionFactory;
    static {
        String resource = "mybatis.xml";
        // 读取配置文件
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 构建会话工厂
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }
    public static SqlSession getsSession(){
        // 通过会话工厂创建会话
        return sqlSessionFactory.openSession();
    }

/*    public static void main(String[] args) {
        SqlSession session = MyBatisHelper.getsSession();
        PlanMapper mapper = session.getMapper(PlanMapper.class);
        mapper.selectById(1);
        session.close();
    }*/

}
